#include "widget.h"
#include "ui_widget.h"
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QDebug>

#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>

#include <QMessageBox>



#include "addstudent.h"
#include "editstudent.h"

#include <QDesktopServices>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->resize(950,500);
    this->setWindowTitle("学员管理系统");

    //初始化表格
    ui->tableWidget->setColumnCount(8);
    ui->tableWidget->setHorizontalHeaderLabels({"学号", "姓名", "昵称", "性别", "年龄", "入学时间", "所属学院","操作"});


    QNetworkAccessManager *manager =new QNetworkAccessManager(this);
    //发送请求得到学员数据
    manager->get(QNetworkRequest(QUrl(this->baseurl + "/student")));
    qDebug()<<this->baseurl + "/student";
    //处理服务器的响应
    connect(manager,&QNetworkAccessManager::finished,[this,manager](QNetworkReply *reply){
        //服务器主题数据 jsonstr
        QString jsonStr=reply->readAll();
        qDebug()<<jsonStr;
        //把json字符串 转为json文档  转为json对象
        QJsonObject jsonObj= QJsonDocument::fromJson(jsonStr.toUtf8()).object();
        //获取data数据  循环添加到表格
        QJsonArray student=jsonObj.value("data").toArray();
        //动态设置行数
        ui->tableWidget->setRowCount(student.size());
        for(int i=0;i<student.size();i++)
        {
            //取出当前循环的单元数据
            QJsonObject tmp= student.at(i).toObject();
            //将数据填充到表格
            ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(tmp.value("sno").toInt())));
            ui->tableWidget->setItem(i,1,new QTableWidgetItem(tmp.value("sname").toString()));
            ui->tableWidget->setItem(i,2,new QTableWidgetItem(tmp.value("snickname").toString()));
            ui->tableWidget->setItem(i,3,new QTableWidgetItem(tmp.value("sgender").toString()));
            ui->tableWidget->setItem(i,4,new QTableWidgetItem(QString::number(tmp.value("sage").toInt())));
            ui->tableWidget->setItem(i,5,new QTableWidgetItem(tmp.value("stime").toString()));
            ui->tableWidget->setItem(i,6,new QTableWidgetItem(tmp.value("dname").toString()));
            //添加操作按钮
            //为每一行添加编辑和删除按钮
            QPushButton *delBtn=new QPushButton("删除");
            QPushButton *editBtn = new QPushButton("编辑");
            // 创建一个空的 QWidget   把水平部件放进widget
            QWidget *w = new QWidget;
            QHBoxLayout *h = new QHBoxLayout(w);
            h->addWidget(delBtn);
            h->addWidget(editBtn);
            //设置水平布局的margin为0，让按钮正常显示处理
            h->setMargin(0);
            //把widget添加到单元格中
            ui->tableWidget->setCellWidget(i,7,w);
            ui->tableWidget->setColumnWidth(7, 200); // 设置单元格宽度


            //删除学员   给每一行绑定信号和槽
            connect(delBtn,&QPushButton::clicked,[this,manager,tmp](){
                //类型 标题 正文 按钮
                //QMessageBox *msgBox = new QMessageBox(QMessageBox::Question, "重要提示", "您确定删除该学员吗?", QMessageBox::Yes | QMessageBox::No);
                QMessageBox msgBox(QMessageBox::Question, "重要提示", "您确定删除该学员吗?", QMessageBox::Yes | QMessageBox::No);
                msgBox.button(QMessageBox::Yes)->setText("确定");
                msgBox.setButtonText(QMessageBox::No,"取消");
                int res = msgBox.exec();
//                int res=QMessageBox::question(this,"删除提醒","您确定删除该学员吗?",QMessageBox::Yes | QMessageBox::No);
                if (res == QMessageBox::Yes)
                {
                    QString url = QString("%1/student/%2").arg(this->baseurl).arg(tmp.value("sno").toInt());
                    manager->deleteResource(QNetworkRequest(QUrl(url)));
                }
            });



            //编辑学员   给每一行绑定信号和槽
            connect(editBtn,&QPushButton::clicked,[this,manager,tmp](){
                EditStudent *editStu=new EditStudent(tmp.value("sno").toInt());
                editStu->show();
                connect(editStu,&EditStudent::updateSucess,[this,manager](){
                    manager->get(QNetworkRequest(QUrl(this->baseurl + "/student")));
                });

            });

        }
    });



    //添加学员
    connect(ui->pushButton,&QPushButton::clicked,[this,manager](){
        addStudent *addStu=new addStudent();
        addStu->show();
        //再次渲染表格   发送请求 服务器响应
        connect(addStu,&addStudent::closeWIdgetFinished,this,[this,manager](){
            manager->get(QNetworkRequest(QUrl(this->baseurl + "/student")));
        });
    });



}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_2_clicked()
{
    QDesktopServices::openUrl(QUrl("http://t.csdnimg.cn/Q5rFL"));
}
